import store from '../../westore/store'
import create from '../../westore/utils/create'

import Config from "../../config"
import Api from "../../utils/api"
var replayer = wx.getBackgroundAudioManager()

// wx.getBackgroundAudioManager  createInnerAudioContext
const musicLengthValues = [0, 3, 5, 10]
create(store, {
  data: {
    openid: "",
    userState: {},
    userSetting: {},
    introVisible: false,
    dateArray: [7, 14, 21],
    musicArray: [1, 2, 3],
    durTimeArray: ['请选择', '3分钟', '5分钟', '10分钟'],
    durSelectIndex: 0,
    canReset: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.watch.durSelectIndex.call(this)
    this.setData({
      userSetting: this.getUserSetting(),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  onHide: function () {
    replayer.stop()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    replayer.stop()

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUserSetting() {
    var len = this.store.data.settingList.length
    if (len > 0) {
      var s = this.store.data.settingList[len - 1]
      this.setData({
        canReset: false,  // 已有设置，没点按钮，防止重置
        durSelectIndex: musicLengthValues.indexOf(parseInt(s.music_length))
      })
      return s
    } else {
      return false
    }
  },
  toggleIntroVisible(e) {
    this.setData({
      introVisible: e.detail
    })
  },

  handleSelect(e) {
    var value = null, cate = e.currentTarget.dataset.cate;
    switch (cate) {
      case "userSetting.music":
        this.setData({
          durSelectIndex: 0
        })
        value = e.currentTarget.dataset.index
        replayer.title = "音乐" + value;
        replayer.src = Config.baseUrl + "musics/3/" + value + "/music_1.mp3";
        replayer.play()
        break
      case "userSetting.days":
        this.setData({
          durSelectIndex: 0
        })
        value = e.currentTarget.dataset.index
        break
      case "durSelectIndex":
        replayer.stop()
        this.setData({
          ["userSetting.music"]: -1,
          ["userSetting.days"]: -1,
        })
        value = e.detail.value
        break
    }
    this.setData({
      [cate]: value
    })
  },
  handleReset() {
    replayer.stop()
    this.setData({
      ["userSetting.music"]: -1,
      ["userSetting.days"]: -1,
      durSelectIndex: 0
    })
  },
  async handleSave() {
    replayer.stop()
    if (!this.data.openid) {
      Api.gotoAuth()
      return
    }
    var us = this.data.userSetting
    if (this.checkFormData()) {
      us.custom = us.music_length ? 1 : 0
      var params = {
        "openid": this.data.openid,
        "custom": us.custom
      }
      if (!us.custom) {
        params.days = us.days
        params.music = us.music
      } else {
        params.type = "day"
        params.music_length = us.music_length
      }
      var res = await Api.setUserSetting(params)
      if (res.status == 0) {
        wx.switchTab({
          url: "/pages/me/me"
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }
  },
  checkFormData() {
    var us = this.data.userSetting
    if (us.music_length || (us.music > -1 && us.days > -1)) {
      return true
    } else {
      wx.showToast({
        title: '请检查您的体验方案',
        icon: 'none'
      })
      return false
    }
  },
  watch: {
    durSelectIndex() {
      var val = this.data.durSelectIndex
      var _this = this
      Object.defineProperty(this.data, "durSelectIndex", {
        configurable: true,
        enumerable: true,
        set(value) {
          _this.setData({
            ["userSetting.music_length"]: musicLengthValues[value]
          })
          val = value;
        },
        get() {
          return val;
        }
      })
    }
  }
})